
//LineBasicMaterial:一种用于绘制线框样式几何体的材质。
//Line:用于将一系列点绘制成一条连续的线。
import * as THREE from 'three';
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { useEffect,memo } from 'react'

const Demo=()=> {
  useEffect(() => {
    /** 创建场景 */
    const scene = new THREE.Scene();

    /** 相机设置 */
    const width = window.innerWidth; //窗口宽度
    const height = window.innerHeight; //窗口高度
    const k = width / height; //窗口宽高比
    const s = 200; //三维场景显示范围控制系数，系数越大，显示的范围越大

    //创建相机对象
    const camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
    camera.position.set(0, 0, 500); //设置相机位置
    camera.lookAt(scene.position); //设置相机方向(指向的场景对象)

    /** 创建渲染器对象 */
    const renderer = new THREE.WebGLRenderer();
    renderer.setSize(width, height); //设置渲染区域尺寸
    //renderer.setClearColor(0x888888, 1); //设置背景颜色
    document.body.appendChild(renderer.domElement); //body元素中插入canvas对象

    function render(){
      renderer.render(scene, camera); //执行渲染操作
    }
    const controls = new OrbitControls(camera, renderer.domElement)
    controls.addEventListener("change", () => {
      render()
    }); //监听鼠标、键盘事件
    const curve = new THREE.SplineCurve( [
      new THREE.Vector2( -10, 0 ),
      new THREE.Vector2( -5, 5 ),
      new THREE.Vector2( 0, 0 ),
      new THREE.Vector2( 5, -5 ),
      new THREE.Vector2( 10, 0 )
    ] );
    // 从一系列的点中，创建一个平滑的二维样条曲线
    const points = curve.getPoints( 50 );
    // 创建一个顶点缓冲几何体
    const geometry = new THREE.BufferGeometry().setFromPoints( points );
    // 线条渲染模式
    const material = new THREE.LineBasicMaterial({
      color: 0xffffff, //线条颜色
    });
    const spline = new THREE.Line( geometry, material );
    scene.add(spline)
    
    render()
    
  }, [])
  return (
    <div className="Demo1"></div>
  )
}
export default memo(Demo)
